package com.yifeng.repo.micro.service.server.engine.listener;

import com.gomcarter.frameworks.base.config.BaseConfProperties;
import com.gomcarter.frameworks.base.converter.PropertyJacksonAnnotationIntrospector;
import com.yifeng.repo.base.utils.common.BaseUtil;
import com.yifeng.repo.base.utils.converter.JacksonHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;

import javax.annotation.Resource;

/**
 * Created by daibing on 2023/3/27.
 */
@Slf4j
public class AppLifeCycleListener implements CommandLineRunner, DisposableBean {
    private static final String YFCLOUD_REPO_VERSION = "1.4.0-SNAPSHOT";
    @Value("${spring.application.name}")
    private String applicationName;
    @Resource
    private BaseConfProperties baseConfProperties;

    @Override
    public void run(String... args) throws Exception {
        // Jackson 支持扩展 @JsonProperty
        JacksonHelper.setAnnotationIntrospector(new PropertyJacksonAnnotationIntrospector(
                baseConfProperties.isDisableJsonProperty(),
                baseConfProperties.getExcludeJsonPropertyClassNamePrefixList()
        ));
        log.info("Startup yfcloud micro service server {}, Enjoy yourself: OwnerId={}, OwnerName={}, yfcloudRepoVersion={}",
                applicationName, BaseUtil.OWNER_ID, BaseUtil.OWNER_NAME, YFCLOUD_REPO_VERSION);
    }

    @Override
    public void destroy() throws Exception {
        log.info("Shutdown yfcloud micro service server {}, Good bye: OwnerId={}, OwnerName={}, yfcloudRepoVersion={}",
                applicationName, BaseUtil.OWNER_ID, BaseUtil.OWNER_NAME, YFCLOUD_REPO_VERSION);
    }
}
